package com.itheima.tcp.demo3;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class SocketThread implements Runnable {
    private Socket accept;
    public SocketThread(Socket accept) {
        this.accept=accept;
    }

    @Override
    public void run() {
        BufferedWriter bw=null;
        try {
            InputStream is =accept.getInputStream();
            BufferedInputStream bis=new BufferedInputStream(is);
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("day16_dcp\\serverDir\\"+ UUID.randomUUID().toString().replace("-","")+".jpg"));
            int b;
            while((b=bis.read())!=-1){
                bos.write(b);
            }
            OutputStream outputStream = accept.getOutputStream();
            bw=new BufferedWriter(new OutputStreamWriter(outputStream));
            bw.write("上传成功");
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {

                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {

                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    }

